<?php
include('rootlogin.php');
//include('../../incsession.php');
session_start();


mysql_query("SET NAMES 'utf8'");
if(!isset($_POST['topic']) || !isset($_POST['year_1']) || !isset($_POST['month_1']) || !isset($_POST['day_1']) || 
							!isset($_POST['year_2']) || !isset($_POST['month_2']) || !isset($_POST['day_2'])){
			$flag=0;
}
else if($_POST['topic'] == '' || $_POST['year_1'] == '' || $_POST['month_1'] == '' || $_POST['day_1'] == '' || 
							$_POST['year_2'] == '' || $_POST['month_2'] == '' || $_POST['day_2'] == '' ||
		$_POST['topic'] == ' ' || $_POST['year_1'] == ' ' || $_POST['month_1'] == ' ' || $_POST['day_1'] == ' ' || 
							$_POST['year_2'] == ' ' || $_POST['month_2'] == ' ' || $_POST['day_2'] == ' '){
			$flag=1;	
}
else{
	$class=$_POST['class'];
	$from_date=$_POST['year_1']."-".$_POST['month_1']."-".$_POST['day_1'];
	$to_date=$_POST['year_2']."-".$_POST['month_2']."-".$_POST['day_2'];
	$t = str_replace("'", "&#39;", $_POST['topic']);
	$t = str_replace('"', '&quot;', $t);
	$query = "UPDATE topic SET name='".$t."', from_date='".$from_date."' , to_date='".$to_date."'   
				WHERE topicid=".$_SESSION['topic'];
	$result = mysql_query($query,$user_con) or die('<div id="error">Error. Please go back to the previous page and try again.</div>');	
	
	$query = "DELETE FROM topic_class 
							WHERE topicid=".$_SESSION['topic'];
	$result = mysql_query($query,$user_con);
	
	for($i=0; $i<sizeof($class); $i++){
		$query = "INSERT INTO topic_class (topicid, classid) VALUES (".$_SESSION['topic'].",".$class[$i].")";
		$result = mysql_query($query,$user_con) or die('<font color="#C11B17">Error. Please go back to the previous page and try again.</font>');	
		//echo $query."<br>";
	}
	
	header ("location: page.php");
}
?>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../framestyle.css" type="text/css" media="screen">
<style type="text/css">

</style>


</head>
<body>
<div id="wrapper">
<div id="title">Online Listening Comprehension Program</div>

<div id="main">

<form method = POST action="topic.php" >

<hr/>
<div id="title2">EDIT A NEW TOPIC</div>
<hr/>
<br><br>

<?php
	if($flag==1){
		echo "<div id='error'>Please fill in all the column.</div><br>";
	}
	echo 'topic name:<br>';
	
	
	
	$query="SELECT * FROM topic WHERE topicid=".$_SESSION['topic'];
	$result = mysql_query($query,$user_con);
	$row = mysql_fetch_array($result);
	echo '<input class="area" type="text" name="topic" value="'.$row['name'].'">';
	
?>
<br><br>
<table width=80%>
<tr>
<td align="right" width=135px>duration: from</td>
<td align="left"> 
<?php

$str=$row['from_date'];
$tok = strtok ($str,"-");
$i=0;
while ($tok) {
	$from[$i]=$tok;
	$tok = strtok ("-"); 
	$i++;
}
echo '<input class="area year" type="text" name="year_1" value="'.$from[0].'"> / ';
echo '<input class="area month_day" type="text" name="month_1" value="'.$from[1].'"> / ';
echo '<input class="area month_day" type="text" name="day_1" value="'.$from[2].'"> &nbsp;<font size=4>example: 2010.10.31</font>';



?>
</td>
</tr>
<tr>
<td align="right" width=135px>to</td>
<td align="left">
<?php
$str=$row['to_date'];
$tok = strtok ($str,"-");
$i=0;
while ($tok) {
	$to[$i]=$tok;
	$tok = strtok ("-"); 
	$i++;
}
echo '<input class="area year" type="text" name="year_2" value="'.$to[0].'"> / ';
echo '<input class="area month_day" type="text" name="month_2" value="'.$to[1].'"> / ';
echo '<input class="area month_day" type="text" name="day_2" value="'.$to[2].'">';
?>
</td>
</tr>
</table>

<br>
classes that will use this topic:
<table>
<tr bgcolor="CCCCCC">
<td width=40px align="center"><input type='checkbox' onClick="javascript:select_all()">
<td width=60px align="center"><font color="FFFFFF" size=4>YEAR</font>
<td width=110px align="center"><font color="FFFFFF" size=4>SEMESTER</font>
<td width=250px align="center"><font color="FFFFFF" size=4>TEACHER</font>
<td width=250px align="center"><font color="FFFFFF" size=4>CLASS NAME</font>


<?php
$query="SELECT * FROM class WHERE classid IN (SELECT classid FROM topic_class WHERE topicid=".$_SESSION['topic'].") AND valid=1 ORDER BY year DESC, semester";
$result = mysql_query($query,$user_con);
$i=0;
while($row[$i] = mysql_fetch_array($result)){
	if($row[$i]['valid']==0)
		$valid="expired";
	else
		$valid="valid";
	echo "<tr><td align='center'><div id='check_".$i."'><input type='checkbox' value=".$row[$i]['classid']." name='class[]' checked></div><td>".$row[$i]['year']."<td>".$row[$i]['semester']."<td>".$row[$i]['teacher']."<td>".$row[$i]['name'];	
	$i++;
}
$query="SELECT * FROM class WHERE classid NOT IN (SELECT classid FROM topic_class WHERE topicid=".$_SESSION['topic'].")  AND valid=1 ORDER BY year DESC, semester";
$result = mysql_query($query,$user_con);
while($row[$i] = mysql_fetch_array($result)){
	if($row[$i]['valid']==0)
		$valid="expired";
	else
		$valid="valid";
	echo "<tr><td align='center'><div id='check_".$i."'><input type='checkbox' value=".$row[$i]['classid']." name='class[]'></div><td><font color='999999'>".$row[$i]['year']."</font><td><font color='999999'>".$row[$i]['semester']."</font><td><font color='999999'>".$row[$i]['teacher']."</font><td><font color='999999'>".$row[$i]['name']."</font>";	
	$i++;
}
?>
<script>

function select_all(){
<?php
//echo "var count=".$i.";";

//$query="SELECT * FROM class WHERE classid IN (SELECT classid FROM topic_class WHERE topicid=".$_SESSION['topic'].") ORDER BY year DESC, semester";
//$result = mysql_query($query,$user_con);
//$i=0;
//while($row = mysql_fetch_array($result)){
for($i=0; $i<sizeof($row[$i]); $i++){
	echo "document.getElementById('check_".$i."').innerHTML='<input type=\"checkbox\" value=".$row[$i]['classid']." name=\"class[]\" checked >';";	
	//$i++;
}
?>

}
</script>
</table>


<br>
<div class="btn_right"><input type="submit" name = "next" value = "submit"></div>
</form>


</div>
</div>
</body>

